#include "game.h"

Game::Game (vector<vector<int>> &_level_data)
{
	init_game(_level_data);
}

Game::Game ()
{
	init_game();
}

void Game::init_game()
{
	_dig_dug = Digdug(45,45,3,3);
//	_monster = Monster;
	_screen_map = generate_default_map();
}

void Game::init_game(vector<vector<int>> &_level_data)
{
	//
	//Level Data contains our objects, stored 
	//The first thign we want to do is generate our objects from the provided data
	//to populate our map from our
	//Preloaded map data.
	
	

	//Now we want to create our movable objects
	
}

void Game::update(Direction dir)
{
	//If keyboard input != shoot
	//Change the direction of digdug
	//then call querymove and pass in digdug
	//
	move_Digdug(_dig_dug, dir);
}


vector<vector<Movable_type>> Game::generate_default_map()
{
	vector<Movable_type> _line_map;
	vector<vector<Movable_type>> _lines_map;
	for(int i =0; i < _grid_size; i++)
	{
		for(int j = 0; j < _grid_size; j++)
		{
			if(i>30 && i<60 && j>30 && j<60)
			_line_map.push_back(SAND);
			else
			_line_map.push_back(HOLE);
		}
		_lines_map.push_back(_line_map);
	}
	return _lines_map;
}

//----------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------
Move_status Game::move_Digdug(Digdug &movable, const Direction &direction)
{
//create vector oftype int to hold return coordinate values
	Move_status final_move_status = CANNOT_MOVE;

	movable.set_direction(direction);

// return the x-location of the center of the object
	int start_movable_center_x = movable.get_x_location(); 
// return the y-location of the center of the object
	int start_movable_center_y = movable.get_y_location();
// get the coordinates and dimensions of the movable
	int start_top_of_movable = start_movable_center_y - movable.get_y_boundary();
	int start_bottom_of_movable = start_movable_center_y + movable.get_y_boundary();
	int start_LHS_of_movable = start_movable_center_x - movable.get_x_boundary();
	int start_RHS_of_movable = start_movable_center_x + movable.get_x_boundary();
	int height_of_movable = 2*movable.get_y_boundary() + 1;
	int length_of_movable = 2*movable.get_x_boundary() + 1;		

	int movable_type;
	movable_type = movable.get_object_type();


	if (movable_type == DIGDUG)
	{
		if (movable.get_direction() == UP) //!!check whether can simply compare an integer to an enumerated type
		{
			
			for (int j = start_top_of_movable - 1; j <= movable.get_speed() && j >= 0; j--)
			{
				for (int i = start_LHS_of_movable; i <= start_RHS_of_movable; i++)
				{
					if( _screen_map[i][j] == SAND)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i][j + height_of_movable +1] = HOLE;
					}

					else if( _screen_map[i][j] == HOLE)
					{					
						_screen_map[i][j] = DIGDUG;
						_screen_map[i][j + height_of_movable +1] = HOLE;
					}
//if DIGUG tries to move into a monster return vector containing [0,0]

					else if(_screen_map[i][j] == MONSTER)
					{
						movable.set_status(DEAD);
						final_move_status = DIED_TRYING_TO_MOVE;
						return final_move_status;
						
						
					}

				}		

			}
			
		}

	}


	int final_movable_center_x = movable.get_x_location();
	int final_movable_center_y = movable.get_y_location();
				
	movable.set_location(final_movable_center_x, final_movable_center_y);

	if(final_movable_center_x == start_movable_center_x  && final_movable_center_y == start_movable_center_y)
	{
		final_move_status = CANNOT_MOVE;
	}
	else
	{
		final_move_status = CAN_MOVE;
	}
				

	return final_move_status;
}

	
